﻿// Copyright (c) Microsoft Corp.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//     http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;

using Microsoft.Health.Mobile;
using System.IO;

namespace TestRegular
{
    [TestFixture]
    public class MobilePlatformTest
    {
        string GetExpectedResultString(uint[] expectedResult)
        {
            byte[] result = new byte[expectedResult.Length * 4];

            for (int i = 0; i < expectedResult.Length; i++)
            {
                byte[] values = BitConverter.GetBytes(expectedResult[i]);

                for (int j = 0; j < 4; j++)
                {
                    result[i * 4 + (3 - j)] = values[j];
                }
            }

            return Convert.ToBase64String(result);
        }

        [Test]
        public void TestSha256Hash()
        {
            string testString = "The quick brown fox jumps over the lazy dog";

            uint[] expectedResult = { 0xd7a8fbb3, 0x07d78094, 0x69ca9abc, 0xb0082e4f, 0x8d5651e4, 0x6d3cdb76, 0x2d02d0bf, 0x37c9e592 };
            string expectedString = GetExpectedResultString(expectedResult);

            string result = MobilePlatform.ComputeSha256Hash(testString);

            Assert.AreEqual(expectedString, result);
        }

        [Test]
        public void TestSha256HashInfoXml()
        {
            string testString = "<info />";

            string expectedString = "ZbR1gwo184c/YSTAFOCI3/p39GNdgAzv8BcW6yK9Ubg=";

            string result = MobilePlatform.ComputeSha256Hash(testString);

            Assert.AreEqual(expectedString, result);
        }

        [Test]
        public void TestLibraryNameLength()
        {
            string abbreviationAndVersion = MobilePlatform.PlatformAbbreviationAndVersion;

            Assert.GreaterOrEqual(abbreviationAndVersion.Length, 7);
            Assert.LessOrEqual(abbreviationAndVersion.Length, 19);
        }

        [Test]
        // RFC 4231 Test vector 4.
        public void TestHmacSha256()
        {
            uint[] expectedResult = { 0xb0344c61, 0xd8db3853, 0x5ca8afce, 0xaf0bf12b, 0x881dc200, 0xc9833da7, 0x26e9376c, 0x2e32cff7 };
            string expectedString = GetExpectedResultString(expectedResult);


            byte[] key = new byte[20];
            for (int i = 0; i < key.Length; i++)
            {
                key[i] = 0x0b;
            }
            string testString = "Hi There";


            string result = MobilePlatform.ComputeSha256Hmac(key, testString);

            Assert.AreEqual(expectedString, result);
        }

        [Test]
        public void TestSaveLoadFile()
        {
            string settingsXml = "<settings><name>Fred</name></settings>";

            MobilePlatform.SaveTextToFile("settings.xml", settingsXml);

            string savedXml = MobilePlatform.ReadTextFromFile("settings.xml");

            Assert.AreEqual(settingsXml, savedXml);
        }

    }
}
